﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DIT.Contract;
using DIT.Domain;
using DIT.Utility;

namespace DIT.Service
{
    public class TaxPayerService : ITaxPayerService
    {
        public TaxPayerService(IRepository _repository)
            : base(_repository)
        {
        }

        public override Guid CreateTaxPayer(Account taxPayer)
        {
            if (taxPayer == null)
                throw new ArgumentNullException("taxPayer");
            return repository.Insert<Account>(taxPayer);
        }

        public override bool ModifyTaxPayer(Account taxPayer)
        {
            if (taxPayer == null)
                throw new ArgumentNullException("taxPayer");
            return repository.Modify<Account>(taxPayer);
        }

        public override void DeleteTaxPayer(Guid taxPayerId)
        {
            repository.Delete<Account>(taxPayerId);
        }

        public override Account GetClientById(Guid taxPayerId)
        {
            return (Account)repository.GetById<Account>(taxPayerId);
        }

        public override Account GetClientByPAN(string pan)
        {
            if (string.IsNullOrWhiteSpace(pan))
                throw new ArgumentNullException("PAN");
            return repository.GetAll<Account>().FirstOrDefault<Account>(t => { return t.PAN == pan; });
        }

        public override IEnumerable<Account> GetClientByLastName(string lastName)
        {
            if (string.IsNullOrWhiteSpace(lastName))
                throw new ArgumentNullException("LastName");
            return repository.GetAll<Account>().Where<Account>(t => { return t.AccountInfo.AssesseeName.SurNameOrOrgName == lastName; });
        }

        public override IEnumerable<Account> GetClientByMobile(ulong mobile)
        {
            return repository.GetAll<Account>().Where<Account>(t => { return t.AccountInfo.MobileNo == mobile; });
        }

        public override IEnumerable<Account> GetAllClient()
        {
            return repository.GetAll<Account>();
        }

        public override IEnumerable<KeyValuePair<EmployerCategoryENUM, string>> GetAllEmployerCategory()
        {
            var v = Enum.GetValues(typeof(EmployerCategoryENUM))
                        .Cast<EmployerCategoryENUM>()
                        .Select<EmployerCategoryENUM, KeyValuePair<EmployerCategoryENUM, string>>(ct => { return new KeyValuePair<EmployerCategoryENUM, string>(ct, DITConfig.AppSettings[ct.ToString()]); });
            return v;
        }

        public override IEnumerable<KeyValuePair<StateCodeENUM, string>> GetAllState()
        {
            var v = Enum.GetValues(typeof(StateCodeENUM))
                        .Cast<StateCodeENUM>()
                        .Select<StateCodeENUM, KeyValuePair<StateCodeENUM, string>>(ct => { return new KeyValuePair<StateCodeENUM, string>(ct, DITConfig.AppSettings[ct.ToString()]); });
            return v;
        }

        public override IEnumerable<KeyValuePair<GenderENUM, string>> GetAllGender()
        {
            var v = Enum.GetValues(typeof(GenderENUM))
                        .Cast<GenderENUM>()
                        .Select<GenderENUM, KeyValuePair<GenderENUM, string>>(ct => { return new KeyValuePair<GenderENUM, string>(ct, DITConfig.AppSettings[ct.ToString()]); });
            return v;
        }

        public override IEnumerable<FilingStatus> GetFilingStatusByClient(Account client)
        {
            return repository.GetAll<FilingStatus>().Where<FilingStatus>(t => { return t.AccountId == client.ID; });
        }

        public override Guid CreateFilingStatus(FilingStatus filingStatus)
        {
            if (filingStatus == null)
                throw new ArgumentNullException("filingStatus");
            return repository.Insert<FilingStatus>(filingStatus);
        }

        public override bool ModifyFilingStatus(FilingStatus filingStatus)
        {
            if (filingStatus == null)
                throw new ArgumentNullException("filingStatus");
            return repository.Modify<FilingStatus>(filingStatus);
        }

        public override FilingStatus GetFilingStatus(Account client, string assesmentYear)
        {
            return repository.GetAll<FilingStatus>().FirstOrDefault<FilingStatus>(t => { return t.AccountId == client.ID && t.AssessmentYear == assesmentYear; });
        }

        public override FilingStatus GetFilingStatusById(Guid filingStatusId)
        {
            return (FilingStatus)repository.GetById<FilingStatus>(filingStatusId);
        }
    }
}
